package com.beeway.blackgoose.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.beeway.blackgoose.R;
import com.beeway.blackgoose.bean.ReultCodeState;
import com.beeway.blackgoose.bean.UserLoginInfo;
import com.beeway.blackgoose.constants.Config;
import com.beeway.blackgoose.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

public class ChangeActivity extends AppCompatActivity implements View.OnClickListener {
    @ViewInject(R.id.change_title_tv)
    private TextView changeTile;
    @ViewInject(R.id.change_content_edt)
    private EditText contentTile;
    @ViewInject(R.id.change_finish_btn)
    private TextView finishBtn;
    private String type;
    @ViewInject(R.id.change_brack_tv)
    private TextView backTv;
    @ViewInject(R.id.change_waim_tv)
    private TextView waimTv;

    private HttpHandler<String> handler;
    private HttpUtils utils;
    private UserLoginInfo info;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        utils = new HttpUtils();
        sp = getSharedPreferences("savepsdfile", MODE_PRIVATE);
        info = (UserLoginInfo) getIntent().getSerializableExtra("info");
        type = getIntent().getStringExtra(Config.COUPON_TYPE);
        if (type != null) {
            if (Config.CHANGE_PHONE.equals(type)) { //这里需要正则表达式验证
                changeTile.setText("修改电话");
                contentTile.setHint("电话号码");
                waimTv.setVisibility(View.GONE);

                finishBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String number = contentTile.getText().toString().trim();
                        if (number != null && !number.equals("")) {
                            if (Utils.isMobileNum(number)) {
                                Intent intent = new Intent();
                                intent.putExtra(Config.CHANGE_PHONE, number);
                                setResult(Config.CHANGE_PHONE_RESULT_CODE, intent);
                                finish();
                            } else {
                                Utils.getToastMessage("手机号格式不正确");
                            }
                        } else {
                            Utils.getToastMessage("手机号不能为空");
                        }
                    }
                });
            } else if (Config.CHANGE_NAME.equals(type)) {
                changeTile.setText("修改昵称");
                contentTile.setHint("昵称");


            } else if (Config.CHANGE_PWD.equals(type)) {
                changeTile.setText("修改密码");
                contentTile.setHint("密码");
                waimTv.setVisibility(View.GONE);
                finishBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String newPwd = contentTile.getText().toString().trim();
                        Utils.getToastMessage("sessionId =" + info.getSessionid() + "oldpwd=" + sp.getString("pwd", "") + " pwd =" + newPwd);
                        setUserChangePed(info.getSessionid(), sp.getString("pwd", ""), newPwd);
                    }
                });

            } else if (Config.CHANGE_EMAIL.equals(type)) {  //这里需要正则表达式验证
                changeTile.setText("修改邮箱");
                contentTile.setHint("邮箱");
                waimTv.setVisibility(View.GONE);
                finishBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String email = contentTile.getText().toString().trim();
                        if (email != null && !email.equals("")) {
                            if (Utils.isEmailNum(email)) {
                                finish();
                            } else {
                                Utils.getToastMessage("邮箱格式不正确");
                            }
                        } else {
                            Utils.getToastMessage("邮箱不能为空");
                        }
                    }
                });
            }
        }
        backTv.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Utils.getAnimOut(this);
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
    }

    //修改密码请求
    public void setUserChangePed(String sessionid, String oldpwd, String newpwd) {
        handler = utils.send(HttpRequest.HttpMethod.POST, Config.CHANGE_UERS_PWD_URL, Config.getUserChangePwdParams(sessionid, oldpwd, newpwd), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                if (result != null) {
                    ReultCodeState state = JSONObject.parseObject(result, ReultCodeState.class);
                    if (Config.STATE_SUCESS.equals(state.getResult())) {
                        Utils.getToastMessage("修改成功");
                    } else if (Config.STATE_FAIL.equals(state.getResult())) {
                        Utils.getToastMessage(state.getMsg() + "ssddff");
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    public void setUserChageName(String name, String sessionid) {
        handler = utils.send(HttpRequest.HttpMethod.POST, Config.CHANGE_UERS_NAME_URL, Config.getUserChangeNameParams(name, sessionid), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}